package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.CourierStationDao;
import com.hs.panghu.entity.Community;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierStationImpl implements CourierStationDao {

    @Override
    public int insertCourierStation(CourierStation courierStation) {
        String sql = "insert into courierstation values (null,now(),?,?,?,?,?,?,?,?,?,?,?)";
        Object object[] = {
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationName(),
                courierStation.getCourierStationOrderCount(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationStatus()
        };
        System.out.println(sql);
        System.out.println(courierStation);
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int updateCourierStation(CourierStation courierStation) {
        String sql = "update courierstation set addTime=now(),courierStationAddress=?,courierStationCity=?,courierStationCommunityId=?," +
                "courierStationCommunityName=?,courierStationImage=?,courierStationDistrict=?,courierStationName=?,courierStationOrderCount=?," +
                "courierStationProvince=?,courierStationSort=?,courierStationStatus=? where courierStationId=?";
        Object object[] = {
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationName(),
                courierStation.getCourierStationOrderCount(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationStatus(),
                courierStation.getCourierStationId()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int deleteCourierStation(Integer courierStation) {
        return 0;
    }

    @Override
    public List<CourierStation> selectCourierStations(Map<String, String> map) {
        List<CourierStation> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select * from courierstation where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !"".equals(courierStationName)) {
            stb.append(" and courierStationName like ? ");
            param.add("%" + courierStationName + "%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        stb.append(" order by courierStationId desc limit ?,? ");
        param.add((pageNum - 1) * pageSize);
        param.add(pageSize);
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                CourierStation courierStation = new CourierStation(
                        resultSet.getInt(1),
                        resultSet.getDate(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getInt(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getInt(10),
                        resultSet.getString(11),
                        resultSet.getInt(12),
                        resultSet.getInt(13)
                );
                list.add(courierStation);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectCourierStationCount(Map<String, String> map) {
        int count = 0;
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select count(1) from courierstation where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String courierStationName = map.get("courierStationName");
        if(courierStationName!=null && !"".equals(courierStationName)){
            stb.append(" and courierStationName like ? ");
            param.add("%"+courierStationName+"%");
        }
        //判断日期是否存在s
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }

    @Override
    public int updateStatus(CourierStation courierStation) {
        String sql = "update courierstation set courierStationStatus=? where courierStationId=?";
        Object obj[] = {
                courierStation.getCourierStationStatus(),
                courierStation.getCourierStationId()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }



}
